/**
 * 
 */
package com.dalonedrau.jogl.opengl;

import com.dalonedrau.jogl.engine.GameApplet;
import com.dalonedrau.jogl.opengl.sprite.FontsheetObject;
import com.dalonedrau.pool.PooledStringBuilder;
import com.dalonedrau.pool.StringBuilderPool;

/**
 * @author Donald
 */
public final class GameFont {
	/** the one and only instance of the <code>GameFont</code> class. */
	private static GameFont	instance;
	/**
	 * Sprite Gives access to the singleton instance of {@link GameFont}.
	 * @return {@link GameFont}
	 */
	public static GameFont getInstance() {
		if (GameFont.instance == null) {
			GameFont.instance = new GameFont();
		}
		return GameFont.instance;
	}
	private FontsheetObject[]	list	= new FontsheetObject[0];
	private String[]			names	= new String[0];
	/** Hidden constructor. */
	private GameFont() {}
	public void addFont(final String name, final int pt,
			final FontsheetObject font) throws Exception {
		if (name == null) {
			PooledStringBuilder sb1 =
					StringBuilderPool.getInstance().getStringBuilder();
			sb1.append("ERROR! GameFont.addFont() - name is null.");
			Exception ex = new Exception(sb1.toString());
			sb1.returnToPool();
			throw ex;
		}
		if (font == null) {
			PooledStringBuilder sb1 =
					StringBuilderPool.getInstance().getStringBuilder();
			sb1.append("ERROR! GameFont.addFont() - font is null.");
			Exception ex = new Exception(sb1.toString());
			sb1.returnToPool();
			throw ex;
		}
		PooledStringBuilder sb1 =
				StringBuilderPool.getInstance().getStringBuilder();
		sb1.append(name);
		sb1.append("_");
		sb1.append(pt);
		boolean found = false;
		for (int i = 0; i < names.length; i++) {
			if (names[i].equals(name)) {
				found = true;
				break;
			}
		}
		if (found) {
			PooledStringBuilder sb2 =
					StringBuilderPool.getInstance().getStringBuilder();
			sb2.append("ERROR! GameFont.addFont() - font '");
			sb2.append(sb1.toString());
			sb2.append("' already created. Duplicate entries are not allowed.");
			Exception ex = new Exception(sb2.toString());
			StringBuilderPool.getInstance().returnObject(sb1);
			StringBuilderPool.getInstance().returnObject(sb2);
			throw ex;
		}
		int index = names.length;
		// extend the names array
		String[] tempStr = new String[index + 1];
		System.arraycopy(names, 0, tempStr, 0, index);
		names = tempStr;
		// extend the fonts array
		FontsheetObject[] tempFont = new FontsheetObject[index + 1];
		System.arraycopy(list, 0, tempFont, 0, index);
		list = tempFont;

		names[index] = sb1.toString();
		list[index] = font;
		StringBuilderPool.getInstance().returnObject(sb1);
	}
	/**
	 * Gets the {@link FontsheetObject} by its reference id.
	 * @param index the font's reference id
	 * @return {@link FontsheetObject}
	 * @throws Exception if the font does not exist
	 */
	public FontsheetObject getFontById(final int index) throws Exception {
		return list[index];
	}
	/**
	 * Gets a specific font by its fully qualified name.
	 * @param name the fully qualified font name
	 * @return {@link FontsheetObject}
	 * @throws Exception if the font doesn't exist
	 */
	public FontsheetObject getFontByName(final String name) throws Exception {
		return getFontById(getFontReferenceId(name));
	}
	/**
	 * Gets a specific font by its name.
	 * @param name the font name
	 * @param pt the font size
	 * @return {@link FontsheetObject}
	 * @throws Exception if the font doesn't exist
	 */
	public FontsheetObject getFontByName(final String name, final int pt)
			throws Exception {
		PooledStringBuilder sb1 =
				StringBuilderPool.getInstance().getStringBuilder();
		sb1.append(name);
		sb1.append("_");
		sb1.append(pt);
		FontsheetObject font = this.getFontByName(sb1.toString());
		sb1.returnToPool();
		return font;
	}
	/**
	 * Gets a font's reference id
	 * @param name the fully qualified font name
	 * @return int
	 * @throws Exception if the font does not exist
	 */
	public int getFontReferenceId(final String name) throws Exception {
		int index = -1;
		for (int i = 0; i < names.length; i++) {
			if (names[i].equals(name)) {
				index = i;
				break;
			}
		}
		if (index == -1) {
			PooledStringBuilder sb1 =
					StringBuilderPool.getInstance().getStringBuilder();
			sb1.append("ERROR! GameFont.getFontReferenceId() - font '");
			sb1.append(name);
			sb1.append("' was never loaded.");
			Exception ex = new Exception(sb1.toString());
			sb1.returnToPool();
			throw ex;
		}
		return index;
	}
	/**
	 * Gets a font's reference id.
	 * @param name the font name
	 * @param pt the font size
	 * @return int
	 * @throws Exception if the font does not exist
	 */
	public int getFontReferenceId(final String name, final int pt)
			throws Exception {
		int index = -1;
		PooledStringBuilder sb1 =
				StringBuilderPool.getInstance().getStringBuilder();
		sb1.append(name);
		sb1.append("_");
		sb1.append(pt);
		index = this.getFontReferenceId(sb1.toString());
		sb1.returnToPool();
		return index;
	}
}
